﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ActionServices;
using CoreData;
using Helper;
using System.IO;

namespace ZidoniCMS
{
    public class BaseController : Controller
    {
        //
        // GET: /Base/
        public static string PathUpload;
        public static string Username;

        public string PermissionString = "";



        public readonly IUserActionService UserService;
        public readonly IBanner_LogoActionService BannerService;
        public readonly ICategoryActionService CategoryService;
        public readonly IProductActionService ProductService;
        public readonly IProductAttributeActionService ProductAttributeService;
        public readonly IProductTypeActionService ProductTypeService;
        public readonly IAttributeActionService AttributeService;
        public readonly IGroupActionService GroupService;     
        public readonly IOrderActionService OrderService;
        public readonly IOrderDetailActionService OrderDetailService;  
        public readonly IProvinceActionService ProvinceService;
        public readonly ISearchActionService SearchService;
        public readonly IArticleActionService ArticleService;
        public readonly IConfigurationActionService ConfigurationService;
        public readonly IConfigurationActionService ConfigService;
        public readonly IDistrictActionService DistrictService;
        public  IUser_Role_ModuleActionService UserRoleModuleService;
        public readonly IUser_GroupActionService UserGroupService;
        public  IModuleActionService ModuleService;
        public readonly IEmailTemplateActionService EmailTemplateService;
        public readonly ICustomerIdeaActionService CustomerIdeaService;
        public readonly IEmailListActionService EmailListService;    
        public readonly ICategoryArticleActionService CategoryArticleService;
        public readonly IAlbumActionService AlbumService;
        public readonly IMenuActionService MenuService;       
        public readonly IOrderServiceActionService Order_serviceService;
        public readonly IShoppingCartActionService ShoppingCartService;
        public readonly IPlansActionService PlansService;
        public readonly IMerchantActionService MerchantService;
        public readonly IMerchant_PaymentActionService MerchantPaymentService;
        public readonly IMerchant_NotifyActionService MerchantNotifyService;


        #region Contructors

        public BaseController() { }

        public BaseController(IMerchant_PaymentActionService merchant_payment)
        {
            this.MerchantPaymentService = merchant_payment;
        }

        public BaseController(IMerchant_NotifyActionService merchant_notify)
        {
            this.MerchantNotifyService = merchant_notify;
        }

        public BaseController(IMenuActionService menu)
        {
            this.MenuService = menu;
        }
        public BaseController(IMerchantActionService merchant)
        {
            this.MerchantService = merchant;
        }

        public BaseController(IPlansActionService plans)
        {
            this.PlansService = plans;
        }

        public BaseController(IMenuActionService menu, IConfigurationActionService _config, 
            ICategoryActionService _category, ICategoryArticleActionService _categoryarticle,IProductActionService _product)
        {
            this.MenuService = menu;
            this.ConfigService = _config;
            this.CategoryService = _category;
            this.CategoryArticleService = _categoryarticle;
            this.ProductService = _product;
        }

        public BaseController(IAlbumActionService album)
        {
            this.AlbumService = album;
        }

        public BaseController(IBanner_LogoActionService _banner) {
            this.BannerService = _banner;
        }
        public BaseController(IBanner_LogoActionService _banner, IAlbumActionService album)
        {
            this.BannerService = _banner;
            this.AlbumService = album;
        }
        public BaseController(IBanner_LogoActionService _banner, IAlbumActionService album, IConfigurationActionService _config)
        {
            this.BannerService = _banner;
            this.AlbumService = album;
            this.ConfigService = _config;
        }
       
        public BaseController (ICategoryActionService _category)
        {
            this.CategoryService = _category;
        }

        public BaseController(ICategoryActionService _category, IProductActionService _product,
            IConfigurationActionService _config, ICategoryArticleActionService cateart, IArticleActionService art)
        {
            this.CategoryService = _category;
            this.ProductService = _product;
            this.ConfigService = _config;
            this.CategoryArticleService = cateart;
            this.ArticleService = art;
        }

        public BaseController(IProductActionService _product, IProductAttributeActionService _attribute,
            ICategoryActionService _category, ISearchActionService _search, IConfigurationActionService _config,
            IEmailTemplateActionService _emailtemplate, IEmailListActionService _emailist)
        {
            this.ProductService = _product;
            this.ProductAttributeService = _attribute;
            this.CategoryService = _category;
            this.SearchService = _search;
            this.ConfigService = _config;
            this.ConfigurationService = _config;
            this.EmailTemplateService = _emailtemplate;
            this.EmailListService = _emailist;
        }
        public BaseController(IProductAttributeActionService _productattribute,
            IConfigurationActionService _config) {
                this.ProductAttributeService = _productattribute;
                this.ConfigService = _config;
        }

        public BaseController(IAttributeActionService _attribute) {
            this.AttributeService = _attribute;
        }

        public BaseController(ICategoryArticleActionService cate)
        {
            this.CategoryArticleService = cate;
        }

        public BaseController(IOrderActionService order,  IUserActionService user)
        {
            this.OrderService = order;
          //  this.OrderDetailService = orderdetail;
            this.UserService = user;
        }
       
        public BaseController(IOrderActionService order,  IUserActionService user,
            IOrderServiceActionService orderservice, IConfigurationActionService config)
        {
            this.OrderService = order;
          //  this.OrderDetailService = orderdetail;
            this.UserService = user;
            this.Order_serviceService = orderservice;
            this.ConfigurationService = config;
        }
        public BaseController(IOrderServiceActionService _orderservice, IUserActionService _user) {
            this.Order_serviceService = _orderservice;
            this.UserService = _user;
        }
        //for Order +++ NTT +++
        public BaseController(IOrderActionService _order, IOrderDetailActionService _orderdetail, IUserActionService _user
            , IProductActionService _product)
        {
            this.OrderService = _order;
            this.OrderDetailService = _orderdetail;
            this.UserService = _user;
            this.ProductService = _product;
        }
        public BaseController(IUserActionService _user, IOrderActionService _order,
            IOrderDetailActionService _orderdetail,
            IProductActionService _product, IEmailListActionService _emaillist,
            IEmailTemplateActionService _mailtemplate, IShoppingCartActionService _shoppingcart)
        { 
            this.UserService = _user; 
            this.OrderService = _order; 
            this.OrderDetailService = _orderdetail; 
            this.ProductService = _product; 
            this.EmailListService = _emaillist; 
            this.EmailTemplateService = _mailtemplate; 
            this.ShoppingCartService = _shoppingcart;
        }
        public BaseController(IConfigurationActionService _service, IEmailTemplateActionService _email)
        { this.ConfigurationService = _service; this.EmailTemplateService = _email; }
        public BaseController(ISearchActionService _search) { this.SearchService = _search; }
        public BaseController(ISearchActionService _search, ICategoryActionService _category) { this.SearchService = _search; this.CategoryService = _category; }
        public BaseController(IUserActionService _user, IUser_GroupActionService usergroup,
            IUser_Role_ModuleActionService rolemodule, IModuleActionService module) 
        { 
            this.UserService = _user;
            this.UserGroupService = usergroup;
            this.UserRoleModuleService = rolemodule;
            this.ModuleService = module;
        }

        public BaseController(IUserActionService _user, IProvinceActionService _province, IEmailListActionService _emaillist) {
            this.UserService = _user;
            this.ProvinceService = _province;
            this.EmailListService =_emaillist;
        }

        public BaseController(IUserActionService _user, IProvinceActionService _province, IEmailListActionService _emaillist,
            IMenuActionService menu, IConfigurationActionService _config)
        {
            this.UserService = _user;
            this.ProvinceService = _province;
            this.EmailListService = _emaillist;
            this.ConfigurationService = _config;
            this.ConfigService = _config;
            this.MenuService = menu;
        }
        public BaseController(IUserActionService _user, IGroupActionService _group, ISearchActionService _search, IUser_Role_ModuleActionService _userrole)
        { 
            this.UserService = _user; this.GroupService = _group;
            this.SearchService = _search;
            this.UserRoleModuleService = _userrole;
        }
        public BaseController(ICategoryActionService _category, IConfigurationActionService _config)
        { this.CategoryService = _category; this.ConfigurationService = _config; }



        public BaseController(ICategoryActionService _category, IConfigurationActionService _config,
            ICategoryArticleActionService cateart, IArticleActionService art)
        {
            this.CategoryService = _category;
            this.ConfigurationService = _config;
            this.ArticleService = art;
            this.CategoryArticleService = cateart;
        }

        public BaseController(IOrderActionService _order)
        {
            this.OrderService = _order;
        }
      
       
        public BaseController(IArticleActionService _article, IUserActionService _user, IConfigurationActionService _config
            ) { this.ArticleService = _article; this.UserService = _user;
            this.ConfigurationService = _config;
            }


        public BaseController(IArticleActionService _article, IUserActionService _user, IConfigurationActionService _config, ICategoryArticleActionService _cate
          )
        {
            this.ArticleService = _article; this.UserService = _user;
            this.ConfigurationService = _config;
            this.CategoryArticleService = _cate;
        }

        public BaseController(IArticleActionService _article) { this.ArticleService = _article; }
        public BaseController(IProvinceActionService _province) { this.ProvinceService = _province; }

        public BaseController(IDistrictActionService district, IProvinceActionService province)
        {
            this.ProvinceService = province;
            this.DistrictService = district;

                
        }

        public BaseController(IUserActionService user, IUser_Role_ModuleActionService userrole,
           IModuleActionService module, IGroupActionService group)
        {
            this.UserService = user;
            this.UserRoleModuleService = userrole;
            this.ModuleService = module;
            this.GroupService = group;
        }
        public BaseController(ICustomerIdeaActionService _customeridea, IUserActionService _user) {
            this.CustomerIdeaService = _customeridea;
            this.UserService = _user;
        }

        public BaseController(ICustomerIdeaActionService _customeridea, IUserActionService _user, ICategoryActionService _category)
        {
            this.CustomerIdeaService = _customeridea;
            this.UserService = _user;
            this.CategoryService = _category;
        }
        public BaseController(IEmailListActionService _emaillist) {
            this.EmailListService = _emaillist;
        }
        public BaseController(IEmailTemplateActionService _emailtemplate)
        {
            this.EmailTemplateService = _emailtemplate;
        }
        public BaseController(IEmailListActionService _emaillist, IEmailTemplateActionService _emailtemplate) {
            this.EmailListService = _emaillist;
            this.EmailTemplateService = _emailtemplate;
        }

        public BaseController(IProductActionService _product, IShoppingCartActionService _shoppingcart)
        {
            this.ProductService = _product;
            this.ShoppingCartService = _shoppingcart;
        }

        #endregion


        public ActionResult SetUpSecurity()
        {
            string user, pass; int userid, groupid = 0;
            if (Session["UserName"] == null)
            {
                Authorization.GetRememberMeCookie(out user, out pass, out userid, out groupid);
                if (!String.IsNullOrEmpty(user) && !String.IsNullOrEmpty(pass))
                {
                    SetPermission(userid, groupid, user);
                }
            }
            if (Session["UserName"] != null)
            {
                groupid = int.Parse(Session["UserGroup"].ToString());
            }
            else
            {
                groupid = 1;

            }
            if (groupid == 1)
            {
                //logout remove all session
                Session["UserName"] = null;
                Session["UserGroup"] = null;
                Session["UserID"] = null;
                Session["url"] = Request.Url.AbsoluteUri;
                return RedirectToAction("Login", "Management", new { returnurl = Request.Url.AbsoluteUri });
            }
            return View();
        }

      public void SetPermission(int userid, int Groupid, string username)
        {
            var ListPermission = new List<CoreData.Permission>();
            if (UserRoleModuleService == null)
                UserRoleModuleService = new User_Role_ModuleActionService();
            if (ModuleService == null)
                ModuleService = new ModuleActionService();
            var permiss = UserRoleModuleService.GetListByLINQ(c => c.UserID == userid);
            foreach (var item in permiss)
            {
                CoreData.Permission p = new CoreData.Permission();
                p.GroupID = Groupid;
                p.Module = ModuleService.GetByID(item.ModuleID).Name;
                p.Role = item.Role;
                p.UserID = userid;
                ListPermission.Add(p);
            }
            SessionManagement.SetSesionValueObject("ListPermission", ListPermission);
            Session["UserName"] = username;
            Session["UserID"] = userid;
            Session["UserGroup"] = Groupid;
        }


        [HttpPost]
        public ContentResult UploadFiles(string dir)
        {
            var r = new List<ViewDataUploadFilesResult>();
            string _file = "";
            foreach (string file in Request.Files)
            {
                HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                if (hpf.ContentLength == 0)
                    continue;
                if(dir.Contains("Category"))
                {
                   _file = FileHelper.FileUpload(hpf, "Media/Category");
                 //  var value = ConfigService.GetOneByLINQ(c => c.Code.Equals("Categoryimg")).Value;
                   var path = Server.MapPath("~/Media/Category/" + Path.GetFileNameWithoutExtension(_file)+Guid.NewGuid().ToString().Substring(0,4)+Path.GetExtension(_file));
                   var _name = Server.MapPath("~/Media/Category/" + _file);
                   _file = Path.GetFileName(_name);// ImageHelper.SaveCompressed(_name, path, long.Parse(value));
                }

                if (dir.Contains("Product"))
                {
                    _file = FileHelper.FileUpload(hpf, "Media/Product");
                  //  var value = ConfigService.GetOneByLINQ(c => c.Code.Equals("Poductimg")).Value;
                    var path = Server.MapPath("~/Media/Product/" + Path.GetFileNameWithoutExtension(_file) + Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(_file));
                    var _name = Server.MapPath("~/Media/Product/" + _file);
                    _file = Path.GetFileName(_name); //ImageHelper.SaveCompressed(_name, path, long.Parse(value));
                }
                if (dir.Contains("ProductAttribute"))
                {
                    _file = FileHelper.FileUpload(hpf, "Media/ProductAttribute");
                   // var value = ConfigService.GetOneByLINQ(c => c.Code.Equals("Poductattributeimg")).Value;
                    var path = Server.MapPath("~/Media/ProductAttribute/" + Path.GetFileNameWithoutExtension(_file) + Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(_file));
                    var _name = Server.MapPath("~/Media/ProductAttribute/" + _file);
                    _file = Path.GetFileName(_name); //ImageHelper.SaveCompressed(_name, path, long.Parse(value));
                }
                if (dir.Contains("Article"))
                {
                    _file = FileHelper.FileUpload(hpf, "Media/Article");
                   // var value = ConfigService.GetOneByLINQ(c => c.Code.Equals("Articleimg")).Value;
                    var path = Server.MapPath("~/Media/Article/" + Path.GetFileNameWithoutExtension(_file) + Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(_file));
                    var _name = Server.MapPath("~/Media/Article/" + _file);
                    _file = Path.GetFileName(_name); //ImageHelper.SaveCompressed(_name, path, long.Parse(value));
                }
                if (dir.Contains("CustomerIdea"))
                {
                    _file = FileHelper.FileUpload(hpf, "Media/CustomerIdea");
                   // var value = ConfigService.GetOneByLINQ(c => c.Code.Equals("CustomerIdeaimg")).Value;
                    var path = Server.MapPath("~/Media/CustomerIdea/" + Path.GetFileNameWithoutExtension(_file) + Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(_file));
                    var _name = Server.MapPath("~/Media/CustomerIdea/" + _file);
                    _file = Path.GetFileName(_name);// ImageHelper.SaveCompressed(_name, path, long.Parse(value));
                }
                if (dir.Contains("BannerLogo"))
                {
                    _file = FileHelper.FileUpload(hpf, "Media/BannerLogo");
                  //  var value = ConfigService.GetOneByLINQ(c => c.Code.Equals("CustomerIdeaimg")).Value;
                    var path = Server.MapPath("~/Media/BannerLogo/" + Path.GetFileNameWithoutExtension(_file) + Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(_file));
                    var _name = Server.MapPath("~/Media/BannerLogo/" + _file);
                    _file = Path.GetFileName(_name);// ImageHelper.SaveCompressed(_name, path, long.Parse(value));
                }
                if (dir.Contains("Tourtype"))
                {
                    _file = FileHelper.FileUpload(hpf, "Media/Tourtype");
                  //  var value = ConfigService.GetOneByLINQ(c => c.Code.Equals("CustomerIdeaimg")).Value;
                    var path = Server.MapPath("~/Media/Tourtype/" + Path.GetFileNameWithoutExtension(_file) + Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(_file));
                    var _name = Server.MapPath("~/Media/Tourtype/" + _file);
                    _file = Path.GetFileName(_name);// ImageHelper.SaveCompressed(_name, path, long.Parse(value));
                }
                if (dir.Contains("Tours"))
                {
                    _file = FileHelper.FileUpload(hpf, "Media/Tours");
                  //  var value = ConfigService.GetOneByLINQ(c => c.Code.Equals("CustomerIdeaimg")).Value;
                    var path = Server.MapPath("~/Media/Tours/" + Path.GetFileNameWithoutExtension(_file) + Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(_file));
                    var _name = Server.MapPath("~/Media/Tours/" + _file);
                    _file = Path.GetFileName(_name);// ImageHelper.SaveCompressed(_name, path, long.Parse(value));
                }
                if (dir.Contains("CateContent"))
                {
                    _file = FileHelper.FileUpload(hpf, "Media/CateContent");
                   // var value = ConfigService.GetOneByLINQ(c => c.Code.Equals("CustomerIdeaimg")).Value;
                    var path = Server.MapPath("~/Media/CateContent/" + Path.GetFileNameWithoutExtension(_file) + Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(_file));
                    var _name = Server.MapPath("~/Media/CateContent/" + _file);
                    _file = Path.GetFileName(_name);// ImageHelper.SaveCompressed(_name, path, long.Parse(value));
                }

                if (dir.Contains("Album"))
                {
                    _file = FileHelper.FileUpload(hpf, "Media/Album");
                  //  var value = ConfigService.GetOneByLINQ(c => c.Code.Equals("CustomerIdeaimg")).Value;
                    var path = Server.MapPath("~/Media/Album/" + Path.GetFileNameWithoutExtension(_file) + Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(_file));
                    var _name = Server.MapPath("~/Media/Album/" + _file);
                    _file = Path.GetFileName(_name);// ImageHelper.SaveCompressed(_name, path, long.Parse(value));
                }

                if (dir.Contains("Menu"))
                {
                    _file = FileHelper.FileUpload(hpf, "Media/Menu");
                  //  var value = ConfigService.GetOneByLINQ(c => c.Code.Equals("CustomerIdeaimg")).Value;
                    var path = Server.MapPath("~/Media/Menu/" + Path.GetFileNameWithoutExtension(_file) + Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(_file));
                    var _name = Server.MapPath("~/Media/Menu/" + _file);
                    _file = Path.GetFileName(_name);// ImageHelper.SaveCompressed(_name, path, long.Parse(value));
                }
                if (dir.Contains("Plans"))
                {
                    _file = FileHelper.FileUpload(hpf, "Media/Plans");

                    var path = Server.MapPath("~/Media/Plans/" + Path.GetFileNameWithoutExtension(_file) + Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(_file));
                    var _name = Server.MapPath("~/Media/Plans/" + _file);
                    _file = Path.GetFileName(_name);// ImageHelper.SaveCompressed(_name, path, long.Parse(value));
                }
                if (dir.Contains("Merchant"))
                {
                    _file = FileHelper.FileUpload(hpf, "Media/Merchant");

                    var path = Server.MapPath("~/Media/Merchant/" + Path.GetFileNameWithoutExtension(_file) + Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(_file));
                    var _name = Server.MapPath("~/Media/Merchant/" + _file);
                    _file = Path.GetFileName(_name);// ImageHelper.SaveCompressed(_name, path, long.Parse(value));
                }
                if (dir.Contains("CarpeDeal"))
                {
                    _file = FileHelper.FileUpload(hpf, "Media/CarpeDeal");

                    var path = Server.MapPath("~/Media/CarpeDeal/" + Path.GetFileNameWithoutExtension(_file) + Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(_file));
                    var _name = Server.MapPath("~/Media/CarpeDeal/" + _file);
                    _file = Path.GetFileName(_name);// ImageHelper.SaveCompressed(_name, path, long.Parse(value));
                }
                PathUpload = _file;
                r.Add(new ViewDataUploadFilesResult()
                {
                    Name = _file,
                    Length = hpf.ContentLength,
                    Type = hpf.ContentType
                });

            }
            return Content("{\"name\":\"" + r[0].Name + "\",\"type\":\"" + r[0].Type + "\",\"size\":\"" + string.Format("{0} bytes", r[0].Length) + "\"}", "application/json");
        }


        public ActionResult Login(string returnurl)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(User _user, string returnurl)
        {
            int userid = 0; string username = ""; int groupid = 0;
            if (UserService.Login(_user, ref username, ref userid, ref groupid) == true)
            {
                SetPermission(userid, groupid, username);
                string decodedUrl = "";
                //remember me
                Authorization.RememberMe(username, _user.Password, userid.ToString(), groupid.ToString());

                if (Session["url"] != null)
                {
                    decodedUrl = Server.UrlDecode(Session["url"].ToString());
                    Session["url"] = null;
                    return Redirect(decodedUrl);
                }
                return RedirectToAction("Index", "Management");
            }
            else
            {
                SessionManagement.SuppendSessions(new string[] { "ListPermission", "UserName", "UserGroup", "UserID" });
                ModelState.AddModelError("", "Tài khoản hoặc mật khẩu không đúng");
                return View(_user);
            }
        }

        public ActionResult LogOff()
        {
            var group = SessionManagement.GetSessionReturnInt("UserGroup");
            if (group > 1)
            {
                Authorization.RemoveRemember();
            }
            SessionManagement.SuppendSessions(new string[] { "ListPermission", "UserName", "UserGroup", "UserID" });
            return RedirectToAction("Login", "Management", new { returnurl = Request.Url.AbsoluteUri });

        }



        public ActionResult CanUpdate(object model = null)
        {

             string user = ""; string pass; int userid; int groupid=0;
             
               if (SessionManagement.GetSessionReturnInt("UserGroup") == 0)
               {
                   Authorization.GetRememberMeCookie(out user, out pass, out userid, out groupid);
                   SessionManagement.SetSesionValue("UserGroup", groupid.ToString());
                   if (SessionManagement.GetSessionReturnToObject("ListPermission") == null)
                   {
                       SetPermission(userid, groupid, user);
                   }
               }
               else
               {
                   groupid = SessionManagement.GetSessionReturnInt("UserGroup");
               }
               if (groupid <= 1)

            {
                Session["url"] = Request.Url.AbsoluteUri;
                return RedirectToAction("Login", "Management", new { returnurl = Request.Url.AbsoluteUri });
            }
            return View(model);
        }
        public ActionResult CanView(object model = null)
        {
            string user = ""; string pass; int userid; int groupid = 0;

            if (SessionManagement.GetSessionReturnInt("UserGroup") == 0)
            {
                Authorization.GetRememberMeCookie(out user, out pass, out userid, out groupid);
                SessionManagement.SetSesionValue("UserGroup", groupid.ToString());
                if (SessionManagement.GetSessionReturnToObject("ListPermission") == null)
                {
                    SetPermission(userid, groupid, user);
                }
            }
            else
            {
                groupid = SessionManagement.GetSessionReturnInt("UserGroup");
            }
            if (groupid <= 1)
            {
                Session["url"] = Request.Url.AbsoluteUri;
                return RedirectToAction("Login", "Management", new { returnurl = Request.Url.AbsoluteUri });
            }
            return View(model);
        }

        public List<CoreData.Permission> Permission
        {
            get
            {
                var data = (List<CoreData.Permission>)SessionManagement.GetSessionReturnToObject("ListPermission");
                return data;
            }
        }

        public int UserID
        {
            get {
                var id = SessionManagement.GetSessionReturnInt("UserID");
                if(id==0)
                { string user = ""; string pass; int userid; int groupid = 0;
                   Authorization.GetRememberMeCookie(out user,out pass,out userid, out groupid);
                   id = userid;
                }
                return id;
            }
        }
        public string UserName
        {
            get
            {
                var username = SessionManagement.GetSessionReturnToString("UserName");
                if (String.IsNullOrEmpty(username))
                {
                    string user = ""; string pass; int userid; int groupid = 0;
                    Authorization.GetRememberMeCookie(out user, out pass, out userid, out groupid);
                    username = user;
                }
                return username;
            }
        }
        public int GroupID
        {
            get
            {
                var group= SessionManagement.GetSessionReturnInt("UserGroup");
                if (group==0)
                {
                    string user = ""; string pass; int userid; int groupid = 0;
                    Authorization.GetRememberMeCookie(out user, out pass, out userid, out groupid);
                    group = groupid;
                }
                return group;
            }
        }
    }
}
